<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container>
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="所属灯杆" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lightPoleId">
              <j-search-select-tag
                placeholder="请选择所属灯杆"
                v-model="model.lightPoleId"
                dict="tb_light_pole,light_name,id"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入设备名称" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceCode">
              <a-input v-model="model.deviceCode" placeholder="请输入设备编号" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceModel">
              <!-- <span>{{ model.deviceModel }}</span> -->
              <a-input v-model="model.deviceModel" placeholder="请输入设备型号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceType">
              <a-select v-model="model.deviceType" :disabled="!isAdd">
                <a-select-option key="6" value="6">led屏</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="!isAdd">
            <a-form-model-item label="在线状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="olStatus">
              <a-select v-model="model.olStatus">
                <a-select-option v-for="o of onlineStatusOptions" :key="o.code" :value="o.code">{{o.name}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="!isAdd">
            <a-form-model-item label="亮度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bri">
              <a-slider v-model="model.bri" :min="1" :max="255" :step="1" :disabled="!isAdd" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="所属项目id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectId">
              <j-search-select-tag v-model="model.projectId" dict="tb_project,project_name,id"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="所属分组id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="groupId">
              <j-search-select-tag v-model="model.groupId" dict="tb_group,group_name,id"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="屏幕高度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="height">
              <a-input v-model="model.height" placeholder="请输入屏幕高度" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="屏幕宽度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="width">
              <a-input v-model="model.width" placeholder="请输入屏幕宽度" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="!isAdd">
            <a-form-model-item
              label="上锁状态"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="screenlockedstatus"
            >
              <span>{{ model.screenlockedstatus | renderScreenlockedStatusTypeName }}</span>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="!isAdd">
            <a-form-model-item
              label="屏幕状态"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="screenonoffstatus"
            >
              <span>{{ model.screenonoffstatus | renderScreenonoffstatusTypeName }}</span>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <a-select v-model="model.status" placeholder="请选择设备状态" v-if="isAdd">
                <a-select-option v-for="o of deviceStatusOptions" :key="o.code" :value="o.code">{{
                  o.name
                }}</a-select-option>
              </a-select>
              <a-select v-model="model.status" placeholder="请选择设备状态" v-else>
                <a-select-option v-for="o of deviceStatusOptions" :key="o.code" :value="o.code">{{
                  o.name
                }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction } from '@/api/manage'
export default {
  name: 'ScreenForm',
  data() {
    return {
      model: {},
      isAdd: true,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      deviceTypeOptions: [
        { code: '1', name: '回路设备' },
        { code: '2', name: '路灯设备' },
        { code: '3', name: '工业设备' },
        { code: '4', name: '气象设备' },
        { code: '5', name: '监控设备' },
        { code: '6', name: 'led屏设备' },
      ],
      onlineStatusOptions: [
        { code: '0', name: '离线' },
        { code: '1', name: '在线' },
      ],
      screenLockOptions: [
        { code: '0', name: '未锁定' },
        { code: '1', name: '锁定' },
      ],
      screenOnOffOptions: [
        { code: '0', name: '关' },
        { code: '1', name: '开' },
      ],
      deviceStatusOptions: [
        { code: '0', name: '禁用' },
        { code: '1', name: '启用' },
        { code: '2', name: '报警' },
      ],
      validatorRules: {
        lightPoleId: [{ required: true, message: '请输入所属灯杆id!' }],
        name: [{ required: true, message: '请输入设备名称' }],
        deviceCode: [{ required: true, message: '请输入设备编号' }],
      },
      url: {
        add: '/corp/led/add',
        edit: '/corp/led/edit',
        queryById: '/corp/led/queryById',
      },
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.isAdd = true
      // this.edit(this.modelDefault)
      this.model = {
        deviceType: '6', //默认led
        status: '1', //默认在线
        olStatus: '1', //设备状态默认启用
        volume: 0, //音量0-100
        bri: 1, //亮度1-255
      }
    },
    edit(record) {
      this.isAdd = false
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
  filters: {
    renderDeviceTypeName(name) {
      const deviceNameMap = {
        1: '回路设备',
        2: '路灯设备',
        3: '工业设备',
        4: '气象设备',
        5: '监控设备',
        6: 'led屏',
      }
      return deviceNameMap[name]
    },
    renderOlStatusTypeName(status) {
      const nameMap = { 0: '掉线', 1: '在线' }
      return nameMap[status]
    },
    renderScreenlockedStatusTypeName(status) {
      const nameMap = { 0: '未锁定', 1: '锁定' }
      return nameMap[status]
    },
    renderScreenonoffstatusTypeName(status) {
      const nameMap = { 0: '关', 1: '开' }
      return nameMap[status]
    },
    renderStatusTypeName(status) {
      const nameMap = { 0: '禁用', 1: '启用', 2: '报警' }
      return nameMap[status]
    },
  },
}
</script>

<style lang="less" scoped>
</style>